var debounce = function (func, wait) {
    var lastArgs;
    var lastThis;   
    var lastCallTime = null;
    var timerId = null;
    function timerExpired() {
        timerId = null
        lastCallTime = Date.now();
        return func.apply(lastThis, lastArgs); 
    }    
    return function (...arg) {
        lastThis = this;
        lastArgs = arg;
        lastCallTime = Date.now();
        if (!timerId) {
            timerId = setTimeout(timerExpired, wait)            
        } else {
            if (Date.now() - lastCallTime < wait) {
                clearTimeout(timerId)
                lastCallTime = Date.now()
                timerId = setTimeout(timerExpired, wait)
            }            
        }
    }
}

